package com.jilefojing.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;

import com.jilefojing.beans.pojos.DCategory;
import com.jilefojing.beans.pojos.EArticle;
import com.jilefojing.beans.vo.Order;
import com.jilefojing.dao.IEArticleDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IEArticleMapper;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年10月03日 16时03分48秒
 */
@Repository
public class EArticleDao extends BaseDao<EArticle> implements IEArticleDao {
	@Autowired
	private IEArticleMapper eArticleMapper;

	@Override
	public List<EArticle> getListByIds(List<Long> ids) {
		Example example = new Example(EArticle.class);
		example.createCriteria().andIn(EArticle.FIELD_ID, ids);
		return super.getListByExample(example);
	}

	@Override
	public List<EArticle> getListByParentId(long parentId) {
		return this.getListByParentIds(Arrays.asList(new Long[] { parentId }));
	}

	@Override
	public List<EArticle> getListByParentIds(List<Long> parentIds) {
		Example example = new Example(EArticle.class);
		example.createCriteria().andIn(EArticle.FIELD_PARENTID, parentIds);
		example.setOrderByClause(EArticle.ORDER_SORT_DESC);
		return super.getListByExample(example);
	}

	@Override
	public EArticle getPrevInDCategory(DCategory dCategory, long articleId,
			List<Order> orders) {
		String categoryCondition = "category_" + dCategory.getCkey() + " = "
				+ dCategory.getId();
		EArticle eArticle = eArticleMapper.getPrevInDCategory(
				categoryCondition, articleId, orders);
		if (eArticle == null) {
			return null;
		}

		return super.getById(eArticle.getId());
	}

	@Override
	public EArticle getNextInDCategory(DCategory dCategory, long articleId,
			List<Order> orders) {
		String categoryCondition = "category_" + dCategory.getCkey() + " = "
				+ dCategory.getId();

		EArticle eArticle = eArticleMapper.getNextInDCategory(
				categoryCondition, articleId, orders);
		if (eArticle == null) {
			return null;
		}

		return super.getById(eArticle.getId());

	}

}
